

import java.util.ArrayList;
import java.util.Collections;
import java.util.Stack;

public class test1 {

    public ArrayList<ArrayList<Integer>> cheng(TreeNode root){
        ArrayList<ArrayList<Integer>> list=new ArrayList<>();
        Stack<TreeNode> stack=new Stack<>();
        stack.push(root);
        TreeNode cur=root;
        boolean flg=true;
        while (cur!=null||!stack.isEmpty()){
            ArrayList<Integer> temp=new ArrayList<>();
            int size=stack.size();
            while (size>0){
                TreeNode cut=stack.pop();
                temp.add(cut.val);
                if(cut.left!=null){
                    stack.push(cut.left);
                }
                if(cur.rihgt!=null){
                    stack.push(cur.rihgt);
                }
                size--;
            }
            if(flg){
                Collections.reverse(temp);
                flg=false;
            }else {
                flg=true;
            }
            list.add(temp);
        }
        return list;
    }
}
